package com.bt.service.impl;

import com.bt.dto.SysMenuDTO;
import com.bt.entity.SysMenu;
import com.bt.mapper.SysMenuMapper;
import com.bt.service.SysMenuService;
import com.bt.vo.SysMenuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Description TODO
 * @Author XJL
 * @Date 2023-05-02 11:38
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 菜单添加
     *
     * @param sysMenuDTO
     */
    @Override
    public void add(SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        sysMenu.setIsShow(sysMenuDTO.getIs_show());
        sysMenu.setIsCache(sysMenuDTO.getIs_cache());
        sysMenu.setIsLink(sysMenuDTO.getIs_link());
        //时间类型转换成Date
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.systemDefault(); // 获取当前时区
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Instant instant = zonedDateTime.toInstant();
        Date date = Date.from(instant);
        //补全数据
        sysMenu.setCreateDate(date);
        sysMenu.setUpdateDate(date);
        sysMenu.setDelFlag(0);
        sysMenu.setDefaultData("1");

        //调用mapper的add方法,插入sys_menu数据库
        sysMenuMapper.add(sysMenu);
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     */
    @Override
    public void update(SysMenu sysMenu) {
        sysMenu.setDefaultData("1");

        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        Instant instant = zonedDateTime.toInstant();
        Date date = Date.from(instant);
        sysMenu.setUpdateDate(date);

        sysMenu.setDelFlag(0);

        sysMenuMapper.update(sysMenu);
    }

    /**
     * 查询所有菜单
     *
     * @return
     */
    @Override
    public List<SysMenuVO> allData() {

        List<SysMenu> menuList = sysMenuMapper.selectList();

        //将查询结果转换为VO类SysMenuVO的集合
        List<SysMenuVO> voList = new ArrayList<>();
        for (SysMenu menu : menuList) {
            SysMenuVO vo = new SysMenuVO();
            BeanUtils.copyProperties(menu, vo);
            voList.add(vo);
        }

        // 通过递归查找每个顶级菜单的子菜单
        List<SysMenuVO> rootList = new ArrayList<>();
        for (SysMenuVO menu : voList) {
            if (menu.getPid() == 0) {
                rootList.add(findChildren(menu, voList));
            }
        }
        //返回根菜单对象集合
        return rootList;
    }

    /**
     * 递归查找菜单的子菜单
     *
     * @param menu     当前菜单
     * @param menuList 菜单列表
     * @return 当前菜单，包含子菜单
     */
    public SysMenuVO findChildren(SysMenuVO menu, List<SysMenuVO> menuList) {
        for (SysMenuVO child : menuList) {
            if (child.getPid().equals(menu.getId())) {
                if (menu.getChildren() == null) {
                    menu.setChildren(new ArrayList<>());
                }
                menu.getChildren().add(findChildren(child, menuList));
            }
        }
        return menu;
    }

    /**
     * 根据id查询菜单(数据回显)
     *
     * @param id
     * @return
     */
    @Override
    public SysMenu rowInfo(Integer id) {
        SysMenu sysMenu = sysMenuMapper.findSysMenuById(id);

        return sysMenu;
    }


    /**
     * 菜单删除
     *
     * @param id
     */
    @Override
    public void del(Integer id) {
        sysMenuMapper.del(id);
    }
}
